package com.knowledgepoint.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knowledgepoint.study.mapper.KnowledgePointCategoryLevelOneMapper;
import com.knowledgepoint.study.model.entity.KnowledgePointCategoryLevelOne;
import com.knowledgepoint.study.service.KnowledgePointCategoryLevelOneService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 一级知识点分类 Service 实现
 */
@Service
public class KnowledgePointCategoryLevelOneServiceImpl extends ServiceImpl<KnowledgePointCategoryLevelOneMapper, KnowledgePointCategoryLevelOne> implements KnowledgePointCategoryLevelOneService {

    @Override
    public List<KnowledgePointCategoryLevelOne> listAll() {
        LambdaQueryWrapper<KnowledgePointCategoryLevelOne> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(KnowledgePointCategoryLevelOne::getSortOrder);
//        queryWrapper.isNull(KnowledgePointCategoryLevelOne::getDeletedAt);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean isNameExist(String name, Integer excludeId) {
        LambdaQueryWrapper<KnowledgePointCategoryLevelOne> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KnowledgePointCategoryLevelOne::getName, name);
        if (excludeId != null) {
            queryWrapper.ne(KnowledgePointCategoryLevelOne::getId, excludeId);
        }
        return baseMapper.selectCount(queryWrapper) > 0;
    }
    
    @Override
    public <P extends IPage<KnowledgePointCategoryLevelOne>> P page(P page, Wrapper<KnowledgePointCategoryLevelOne> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }
    
    @Override
    public KnowledgePointCategoryLevelOne getDetailById(Integer id) {
        return baseMapper.selectById(id);
    }
    
    @Override
    public boolean add(KnowledgePointCategoryLevelOne category) {
        // 如果没有设置排序顺序，自动设置为当前最大值+1
        if (category.getSortOrder() == null) {
            // 查询当前最大的排序值
            LambdaQueryWrapper<KnowledgePointCategoryLevelOne> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByDesc(KnowledgePointCategoryLevelOne::getSortOrder);
            queryWrapper.last("LIMIT 1");
            KnowledgePointCategoryLevelOne maxSortCategory = baseMapper.selectOne(queryWrapper);
            
            // 设置新分类的排序值为最大值+1，如果没有分类则设置为1
            int sortOrder = (maxSortCategory != null && maxSortCategory.getSortOrder() != null) 
                    ? maxSortCategory.getSortOrder() + 1 
                    : 1;
            category.setSortOrder(sortOrder);
        }
        return save(category);
    }
    
    @Override
    public boolean update(KnowledgePointCategoryLevelOne category) {
        return updateById(category);
    }
    
    @Override
    public boolean removeById(Integer id) {
        return super.removeById(id);
    }
}
